package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonres.view.AutoRadioGroup;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerPersonUpdateComponent;
import com.eduhzy.ttw.teacher.mvp.contract.PersonUpdateContract;
import com.eduhzy.ttw.teacher.mvp.presenter.PersonUpdatePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 */
@Route(path = RouterHub.APP_PERSONUPDATEACTIVITY)
public class PersonUpdateActivity extends BaseActivity<PersonUpdatePresenter> implements PersonUpdateContract.View {

    @BindView(R.id.person_name)
    XEditText personName;
    @BindView(R.id.rg_gender)
    AutoRadioGroup rgGender;
    @Autowired(name = Constants.ROUTER_STRING)
    String name = null;
    @Autowired(name = Constants.ROUTER_INTEGER)
    int gender = 0;
    private QMUITipDialog dialog;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerPersonUpdateComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_person_update;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (ObjectUtils.isNotEmpty(name) && gender == 0) {
            personName.setVisibility(View.VISIBLE);
            personName.setText(name);
            rgGender.setVisibility(View.GONE);
        } else {
            if (gender > 0 && gender <= 2) {
                ((RadioButton) rgGender.getChildAt(gender - 1)).setChecked(true);
            }
            personName.setVisibility(View.GONE);
            rgGender.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoading() {
        if (dialog == null) {
            dialog = RxUtil.showLoading(this, "请稍后···");
        }
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(dialog)) {
            dialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnCheckedChanged({R.id.rb_male, R.id.rb_female})
    public void onCheckedChanged(CompoundButton button, boolean checked) {
        if (checked) {
            switch (button.getId()) {
                case R.id.rb_male:
                    gender = 1;
                    break;
                case R.id.rb_female:
                    gender = 2;
                    break;
                default:
            }
        }
    }

    @OnClick(R.id.save_btn)
    public void onViewClicked() {
        KeyboardUtils.hideSoftInput(this);
        if (ObjectUtils.isNotEmpty(name)&&gender==0) {

            String newName = personName.getText().toString();
            if (StringUtils.isTrimEmpty(newName)) {
                RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入您的姓名！", 1500);
                return;
            }
            mPresenter.updateUser(newName, 0);
        }else{
            mPresenter.updateUser(null, gender);
        }

    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
